Namespace Actions.ActionItems
    <ActionItemWordAttribute("filelist")> _
    Public Class aiFileList
        Inherits ActionItem
        Public RsName As String = "default"
        Public DirectoryName As String = "d:\"
        Public Delimiter As String = ";"

        Public FileNameLength As Integer = 255

        Private rs As New ADODB.Recordset

        Public Sub New(ByVal Owner As Action, ByVal Definition As Xml.XmlNode)
            MyBase.New(Owner, Definition)
            rs.Fields.Append("FilePath", ADODB.DataTypeEnum.adLongVarChar, 4096)
            rs.Fields.Append("FileName", ADODB.DataTypeEnum.adVarChar, FileNameLength)
            rs.Fields.Append("FileSize", ADODB.DataTypeEnum.adBigInt)
            rs.Fields.Append("FileDate", ADODB.DataTypeEnum.adDBDate)
            rs.Fields.Append("FileTime", ADODB.DataTypeEnum.adDBTime)
            rs.Open()
        End Sub

        Public Overrides Sub Execute()
            RsName = Configuration.Attribute("name", "default")
            DirectoryName = Configuration.Attribute("directory", "")

            DirectoryName = OwnerAction.InsertParams(DirectoryName)
            Dim files() As String = IO.Directory.GetFiles(DirectoryName)

            OwnerAction.Resources.Recordsets(RsName) = rs

            Dim FileName As String

            Dim ShouldWriteLine As Boolean = False
            For Each FullFileName As String In files
                Dim f As New IO.FileInfo(FullFileName)

                If f.Name.Length > FileNameLength Then
                    FileName = f.Name.Substring(0, FileNameLength)
                Else
                    FileName = f.Name
                End If

                rs.AddNew()
                rs("FilePath").Value = f.FullName
                rs("FileName").Value = FileName
                rs("FileSize").Value = f.Length
                rs("FileDate").Value = f.LastWriteTime.ToString("yyyy-MM-dd")
                rs("FileTime").Value = f.LastWriteTime.ToString("hh:mm:ss")
                rs.Update()
                Threading.Thread.Sleep(1)
            Next
        End Sub
    End Class
End Namespace